// window.onload = function(){
// url网站域名
var url="http://hengzhi.ayvvv.com";
// 257多样式
//加载指定商品开始
var token=localStorage.getItem('token');
var pcount='';
var product_id = getUrlParam('product_id');
console.log( product_id,"111111111")
function xhxz(i){
    $('.noselected'+i).click( function () { 
        $('.noselected'+i).removeClass("selected");
        $(this).addClass("selected");
    }); 
}

$.ajax({
    url:url + '/api/store/product/detail/'+product_id, 
    dataType:"json",
    type:"get",
    async:true,
    success:function(product_data){
        console.log(product_data,"222222222")
         //用户图像的获取
         $("#userimage").attr('src',product_data.data.topReply.avatar)
         //用户名称的获取
         $("#username").html(product_data.data.topReply.nickname)
         //用户评分的获取
         $("#score").html(product_data.data.topReply.product_score)
         //用户时间的获取
         $("#timeid").html(product_data.data.topReply.create_time)
         //用户评价内容
         $("#discuss").html(product_data.data.topReply.comment)
            // console.log(product_data.data.stock);
            // 商品库存
           pcount=product_data.data.stock
           // 热销/店铺推荐开始
           var hp_arry=product_data.data.merchant.recommend;

           for (var i = 0; i < hp_arry.length; i++) {

            $('#hot-sale').append(`<li><a href="goods.html?product_id=`+hp_arry[i].product_id+`">`+hp_arry[i].store_name+`</a></li>`);
            
           }
           // 热销/店铺推荐结束
        // 主商品信息加载开始

           // 商品款式加载开始
           // console.log(product_data.data.attr);

           var pd_arry=product_data.data.attr;

           // for (var i = 0; i < pd_arry.length; i++) {
           for (var a = pd_arry.length; a > 0; a--) {
               var i=a-1;
               var arr1=pd_arry[i].attr_values;
               // console.log(pd_arry[i]);
               $('#product_style').prepend(`<li class="product">
                                <dl>
                                    <dt>`+pd_arry[i].attr_name+`：</dt>
                                    <dd id="product_styles">                             
                                    </dd>
                                </dl>
                            </li>`);

               for (var j = 0; j < arr1.length; j++){
                    if (j==0) {$('#product_styles').append(`<a class="selected noselected`+i+`" href="javascript:;">`+arr1[j]+` <input class="xhl`+i+`" type="radio" name="xhl`+i+`" value="`+arr1[j]+`" checked="checked" /></a>`);}
                    else{$('#product_styles').append(`<a class="noselected`+i+`" href="javascript:;">`+arr1[j]+` <input type="radio" class="xhl`+i+`" name="xhl`+i+`" value="`+arr1[j]+` " /></a>`);}
               }
                xhxz(i)

           }
 

    
           // 商品款式加载结束
           $('.store_name').text(product_data.data.store_name);
           $('#store_info').text(product_data.data.store_info);
           $('#store_price').text('￥'+product_data.data.price);
           $('#store_ot_price').text('￥'+product_data.data.ot_price);
           // 商品编号加载开始
           let arr = Object.values(product_data.data.sku)
           // 商品编号为固定第一种产品的编号
           // console.log(arr);
           $('.store_product_id').text(`商品编号：`+arr[0].bar_code);
           // 商品编号加载结束
           // 商品评论加载开始
           // console.log(product_data.data.content.content);
           $('.desc').append(product_data.data.content.content);
           // 商品评论加载结束

           var img_arry=product_data.data.slider_image;
           $('#store_images_a').attr({ href: img_arry[0]});
           $('#store_images_img').attr({ src: img_arry[0]});
            for (var i = 0; i < img_arry.length; i++) {
                var j=i+1;
                    if (i==0){
                    $('#store_image').append(`<li class="cur">
                                    <a class="zoomThumbActive click_xz"  href="javascript:void(0);" rel="{gallery: 'gal1', smallimage: '`+img_arry[i]+`',largeimage: '`+img_arry[i]+`'}">
                                    <img src="`+img_arry[i]+`"></a>
                                </li>`);
                    }else{
                    $('#store_image').append(`<li>
                                    <a class="click_xz" href="javascript:void(0);" 
                                    rel="{gallery: 'gal1', smallimage: '`+img_arry[i]+`',largeimage: '`+img_arry[i]+`'}">  
                                    <img src="`+img_arry[i]+`"></a>
                                </li>`);
                    }

            };
            $('.click_xz').click( function () { 
                $('.zoomThumbActive').attr({class: ""});
                $(this).attr({class: "zoomThumbActive"});
                var img_url=$(this).find("img").attr('src');
                // $('#store_images_a').attr({ href: img_url});
                $('#store_images_img').attr({ src: img_url});
            });
        // 主商品信息加载开始结束


                  // 加入购物车开始
                  $(".add_btn").click( function () {    
                    var store_style=store_style=$('.selected').text().split(' ');
                    var store_clieck=store_style.join(",")
                    var store_str=store_clieck.substring(0,store_clieck.length-1)
                    var unique=product_data.data.sku[store_str].unique;
                    var cart_num=$('.amount').val();

                    if (pcount==0) {layer.alert("该商品正在补货中");}else{
                         $.ajax({
                             url:url+'/api/user/cart/create',
                             dataType:"json",
                             type:"post",
                             headers:{
                                 "X-Token":token,
                             },
                             data:{"product_id": product_id,"product_attr_unique": unique,"cart_num": cart_num},
                             async:true,
                             success:function(result){

                                 if(token&&result.status==200){                                     
                                         layer.alert("购物车加入成功");
                                                        
                                 }else{
                                         layer.alert(result.message);
                                 }
                             },
                                error:function(){
                               
                             }
                         });
                     }
             });
             // });
         // 加入购物车结束

         //立即购买开始
         $("#goshop").click( function () { 
             var store_style=store_style=$('.selected').text().split(' ');
             var store_clieck=store_style.join(",")
             var store_str=store_clieck.substring(0,store_clieck.length-1)
             var unique=product_data.data.sku[store_str].unique;
             var cart_num=$('.amount').val();   

             if (pcount==0) {layer.alert("该商品正在补货中");}else{
                 $.ajax({
                 url:url+'/api/user/cart/create',
                     dataType:"json",
                     type:"post",
                     headers:{
                         "X-Token":token,
                     },
                     data:{"product_id": product_id,"product_attr_unique": unique,"cart_num": cart_num},
                     async:true,
                     success:function(result){
                         console.log(result);
                         if(token&&result.status==200){
                             window.location.href="flow2.html?sel_id="+result.data.cart_id
                             // window.location.href="flow1.html";
                                                
                         }else{
                             layer.alert("原因"+result.message);
                             
                         }
                     },
                        error:function(){
                       
                     }
                 });
             }
             });
             // });
         //立即购买结束



    },      
    error:function(){
    }
    });
//加载指定商品结束

// 相关分类，推荐品牌开始
var fl_1=2000;
var fl_2=2020;

var store_category_id = getUrlParam('cate_id');
$.ajax({
    url:url + '/api/store/product/brand/lst',
    dataType:"json",
    type:"get",
    data:{cate_id: store_category_id},
    async:true,
    success:function(sort_data){
           // console.log(sort_data.data.list[0].brand_name);
           var sb_arry=sort_data.data.list;
           for (var i = 0; i < sb_arry.length; i++) {
              $('#same-brand').append(`<li><a href="同类品牌ID"`+sb_arry[i].brand_id+`">`+sb_arry[i].brand_name+`</a></li>`);
           }

    },
       error:function(){
    }
    });

$.ajax({
    url:url + '/api/store/product/category/lst',
    dataType:"json",
    type:"get",
    async:true,
    success:function(sort){
           for (var i = 0; i < sort.data.length; i++) {
                if (sort.data[i].store_category_id==fl_1) {
                    var arr=sort.data[i].children;
                    for (var i = 0; i <arr.length; i++) {
                        if (arr[i].store_category_id==fl_2) {
                            var arr1=arr[i].children;
                            // console.log(arr1);
                            for (var i = 0; i <4; i++) {
                                $('#relevant-class').append(`<li><a href="相关分类id"`+arr1[i].store_category_id+`>`+arr1[i].cate_name+`</a></li>`);
                                }  
                        }
                        return;
                    }
                    return;
                }
           }

    },
       error:function(){
    }
    });
// 相关分类，推荐品牌结束





































































































// }

